package generatoreLALR;
import asd.Demo8;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import tree.NTree;
import tree.Node;
import tree.Nodo;
import tree.Tree;
import tree.TreeUtil;

/**
 *
 * @author Giovanni Cammarata
 */
public class Main {
    static NTree tree;



	public static void main(String args[]) throws IOException {
                
            StringReader str;
			
            NTree<Nodo> tree;
            
		Parser yyparser;
		
		if (args.length==0) 
                    {
                        while(true) {
                            tree = new NTree<>();
                            System.out.print("Inserisci espressione ");
                            BufferedReader variabile = new BufferedReader(new InputStreamReader(System.in));
                            String strletta = variabile.readLine()+"\n";
                            str = new StringReader(strletta);
                            yyparser = new Parser(str,false,tree);
                            yyparser.yyparse();
                            buildASD(tree);
                        }
                        
                    }
		else  {
                            tree = new NTree<>();
                            
                            String strletta;
                            FileInputStream fstream = new FileInputStream("file2.txt");
                            DataInputStream in = new DataInputStream(fstream);
                            BufferedReader br = new BufferedReader(new InputStreamReader(in));
                            String strLine;
                            while ((strletta = br.readLine()) != null) {
                                tree = new NTree<>();
                                str = new StringReader(strletta);
                                yyparser = new Parser(str,false,tree);
                                yyparser.yyparse();
                                buildASD(tree);
                                tree = new NTree<>();
                                }

                            in.close();
                            
                      }
		
                
                       
                

                
                
                //util.stampa(tree);
                
                
                
//                JTree jtree = new JTree();
//                
//                    
//                    jtree = buildTree(tree);
//                    jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
//                    jtree.setShowsRootHandles(true);
//                    jtree.expandPath(null);
//                    JPanel panel = new JPanel();
//                    
//                    JFrame frame = new JFrame("ASD ");
//                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//                    frame.setUndecorated(true);
//                    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
//                    //frame.setSize(200,300);
//                    frame.setBounds(210, 10, 200, 500);
//                    
//                    frame.setVisible(true);
//                    
//                    
//                    frame.add(jtree);
                    
                
                
                
                
                
                
                          
	}
        
        
        private static JTree buildTree(NTree<Nodo> treeP) {            
                TreeUtil<Nodo> util = new TreeUtil<Nodo>();
                return util.buildJTree(treeP);
        }
        
        private static void buildASD(NTree<Nodo> treeP) {  
            TreeUtil<Nodo> util = new TreeUtil<Nodo>();
                if(!treeP.isEmpty()) {
                    util.stampaNodi(treeP);
                    Demo8 prova = new Demo8(treeP);
                }
        }

}

